package com.mediacloud.app.newsmodule.fragment.audio.live;

import android.content.Intent;
import android.content.IntentFilter;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.view.View;
import android.widget.AdapterView;

import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.adaptor.video.live.LiveProgramAdaptor;
import com.mediacloud.app.newsmodule.adaptor.video.live.LiveProgramAdaptor1;
import com.mediacloud.app.newsmodule.adaptor.video.live.LiveProgramChatAdaptor;
import com.mediacloud.app.newsmodule.broadcast.LiveReplayBroadcast;
import com.mediacloud.app.newsmodule.fragment.video.live.LiveProgramItemFragment;
import com.mediacloud.app.model.news.ProgramListItem;
import com.mediacloud.app.newsmodule.fragment.video.live.LiveProgramTableFragment;
import com.mediacloud.app.newsmodule.utils.ToastUtil;

import java.util.List;

/**
 * 音频直播节目单列表
 * @author x
 *
 */
public class AudioProgramItemFragment extends LiveProgramItemFragment {

	public View.OnClickListener closeListener;
	@Override
	protected void initOther() {
		/**
		 * 半屏模式的音频节目单
		 * */
		boolean fullScreen = getFragmentArguments().getBoolean("fullScreen");
		final boolean chatStyle =  getFragmentArguments().getBoolean("chatStyle");
		if(chatStyle){
			liveProgramAdaptor = new LiveProgramChatAdaptor(getActivity());
		}else{
			if (!fullScreen) {
				liveProgramAdaptor = new LiveProgramAdaptor1(getActivity(),true);
			} else {
				liveProgramAdaptor = new LiveProgramAdaptor(getActivity());
			}
		}
		programList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				position = position - programList.getHeaderViewsCount();
				ProgramListItem item = liveProgramAdaptor.getItem(position);
				//如果选择的是空 或者  选中了当前的 或者 是即将播放的就不处理
				if(ProgramListItem.GuideItemState.COMING_SOON == ProgramListItem.getItemState(item)){
					ToastUtil.show(getContext(),"还没到播放时间");
					return;
				}
				if(chatStyle){
					if (item == null) {
						return;
					}
				}else {
					if (item == null || liveProgramAdaptor.selectedIndex == position) {
						return;
					}
				}
				liveProgramAdaptor.selectedIndex = position;
				liveProgramAdaptor.notifyDataSetChanged();
				Intent intent = new Intent();
				intent.setAction(LiveReplayBroadcast.PlayAction);
				intent.putExtra("data", item);
				LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);//发送广播  通知回看或是从回看切到直播
				sendUpdateChoose();
				if(closeListener!=null)
				{
					closeListener.onClick(view);
				}
				if(chatStyle){
					getActivity().finish();
				}
			}
		});

		liveReplayBroadcast = new LiveReplayBroadcast();
		liveReplayBroadcast.updateListState = this;
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(LiveReplayBroadcast.UpdateState);
		intentFilter.addAction(LiveReplayBroadcast.UpdateChoose);
		getActivity().registerReceiver(liveReplayBroadcast, intentFilter);

		List<ProgramListItem> list = (List<ProgramListItem>) getFragmentArguments().getSerializable(LiveProgramTableFragment.Program);
		if (list != null) {
			setListData(list);
		}
		liveProgramAdaptor.replayStrId=R.string.audio_replay;
	}
	/**
	 * 这重写 把正在播放的节目 发送出去 让主界面更新当前的播放内容 或者在其他地方去刷 不走这的通道
	 */
	@Override
	protected ProgramListItem updateLiveLabelDelay(int idx) {
		ProgramListItem item=super.updateLiveLabelDelay(idx);
		return item;
	}
}